<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@page import="db.ReceiveorderDAO"%>
<%@page import="db.ReceivepurmatDAO"%>
<%@page import="db.ProjectDAO"%>
<%@page import="java.text.SimpleDateFormat"%>
<%@ include file="/jsp/common/utility/volti.jsp" %>
<%@ include file="/jsp/common/utility/logined.jsp"%>
<%
	WebApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
	ReceiveorderDAO receiveorderDao = (ReceiveorderDAO)ctx.getBean("ReceiveorderDAO");
	ReceivepurmatDAO receivepurmatDao = (ReceivepurmatDAO)ctx.getBean("ReceivepurmatDAO");
	ProjectDAO projectDao = (ProjectDAO)ctx.getBean("ProjectDAO");
	UserDAO userDao = (UserDAO)ctx.getBean("UserDAO");
	
	String searchType = request.getParameter("searchType");
	String searchKeyword = request.getParameter("searchKeyword");
	String beginDate = request.getParameter("beginDate");
	String endDate = request.getParameter("endDate");
	
	int pn = 0;
	int pageSize = 0;
	Integer uid = (Integer)session.getAttribute("id");
	Integer teamId = (Integer)session.getAttribute("tid");
	
	User user = userDao.findById(uid);
	request.setAttribute("user",user);
	
	List where = new ArrayList();
	
	where.add(new Object[]{"eq", "isDelete", 0});
	where.add(new Object[]{"eq", "teamId", teamId});
	
	if(searchType != null && !"".equals(searchType)){
		if("1".equals(searchType))
			where.add(new Object[]{"like", "m.code", searchKeyword});
		else if("2".equals(searchType))
			where.add(new Object[]{"like", "m.name", searchKeyword});
	}
	
	if(beginDate != null && !"".equals(beginDate)){
		Date dateVal = new SimpleDateFormat("yyyy-MM-dd").parse(beginDate);
		where.add(new Object[]{"ge", "date", dateVal});
	}
	
	if(endDate != null && !"".equals(endDate)){
		Date dateVal = new SimpleDateFormat("yyyy-MM-dd").parse(endDate);
		where.add(new Object[]{"le", "date", dateVal});
	}
	
	if(searchKeyword != null && !"".equals(searchKeyword)){
		Map map = receivepurmatDao.getReceivepurmat(pn, pageSize,
				new String[][]{new String[]{"desc","date"}},
				where,
				new String[][]{new String[]{"","id"},new String[]{"","m.code"},new String[]{"","m.name"},new String[]{"","s.name"},new String[]{"","pr.code"},new String[]{"","batch"},new String[]{"","m.company"},new String[]{"","purPrice"},new String[]{"","purNum"},new String[]{"","date"}}		
				,new String[]{"receiveorder","place","material","substore","project"});
		
		request.setAttribute("map",map);
		
		int pageTotal = ((Integer)map.get("pageTotal")).intValue();
		int pageNo = ((Integer)map.get("pageNo")).intValue();
		List pageList=getPage(pageTotal,pageNo);
		request.setAttribute("pages",pageList);
	}
	
	request.setAttribute("searchType",searchType);
	request.setAttribute("searchKeyword",searchKeyword);
	request.setAttribute("beginDate",beginDate);
	request.setAttribute("endDate",endDate);

	pageContext.forward("/vm/statistics/report_3.vm");
%>
